package org.jsbd.boss.util;

import java.util.List;

import org.jsbd.boss.bto.HttpReq;
import org.jsbd.boss.bto.SdkSmsVo;
import org.jsbd.boss.bto.SmsProtocol;
import org.jsbd.boss.common.BusType;
import org.jsbd.boss.common.ConstantsCmp;
import org.jsbd.boss.enums.SmsSendTypeEnum;

import com.alibaba.fastjson.JSONObject;

import edu.hziee.cap.common.util.Md5Utils;

/**
 * 
 * @author <a href="mailto:zhangbinghua@zhexinit.com" >zbh</a>
 * @version 1.0.0
 */
public class DMChannelSmsUtil {
	private static int timeout = 5000;

	public static HttpReq flushChannel(String type, String uuid, SdkSmsVo smsVo) {
		HttpReq httpReq = new HttpReq();
		httpReq.setType(type);
		httpReq.setTimeoutConnect(timeout);
		httpReq.setTimeoutRead(timeout);
		httpReq.setId(uuid);
		httpReq.setParam(smsVo);
		return httpReq;
	}

	public static SmsProtocol wrapSmsProtocol(List<HttpReq> httpReqList) {
		SmsProtocol smsProtocol = new SmsProtocol();
		smsProtocol.setFlowType(ConstantsCmp.PROTOCOL_FLOW_TYPE_INTERRUPT); // 这个请求是需要中断的
		smsProtocol.setResultCode(ConstantsCmp.PROTOCOL_CONTINUE_RESULT_CODE);
		smsProtocol.setNeedDetailResultCode(ConstantsCmp.PROTOCOL_NOT_NEED_DETAIL_RESULT_CODE);// 不需要详细信息
		smsProtocol.setBusType(BusType.DM_CHANNEL_SMS.getCode());//
		smsProtocol.setList(httpReqList);
		return smsProtocol;
	}

	/**
	 * 
	 * createSms:生成短信
	 * 
	 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
	 * @param sysTime
	 * @param sendType
	 * @param andriodId
	 * @return
	 */
	public static String createSms(String sysTime, Integer sendType, String andriodId) {
		JSONObject param = new JSONObject();
		param.put("phone", "106581061");
		String message = 0 + EncUtil.toMd5(sysTime + andriodId) + "[中移统一认证]";
		if (sendType != null && sendType == 1) {
			param.put("sendType", String.valueOf(SmsSendTypeEnum.TEXT.getType()));
			param.put("message", message);
			param.put("sendPort", "0");
		} else {
			param.put("sendType", String.valueOf(SmsSendTypeEnum.DATA.getType()));
			param.put("message", AndroidBase64.encode(message.getBytes()));
			param.put("sendPort", "2000");
		}
		return param.toJSONString();
	}

	/**
	 * 
	 * createSms:生成短信
	 * 
	 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
	 * @param sysTime
	 * @param sendType
	 * @param andriodId
	 * @return
	 */
	public static String createSms(String sysTime, Integer sendType, String andriodId, Integer apkId) {
		JSONObject param = new JSONObject();
		param.put("phone", "106581061");
		String message = 0 + EncUtil.toMd5(sysTime + andriodId) + "[中移统一认证]";
		if (sendType != null && sendType == 1) {
			param.put("sendType", String.valueOf(SmsSendTypeEnum.TEXT.getType()));
			param.put("message", message);
			param.put("sendPort", "0");
		} else {
			param.put("sendType", String.valueOf(SmsSendTypeEnum.DATA.getType()));
			param.put("message", AndroidBase64.encode(message.getBytes()));
			param.put("sendPort", "2000");
		}
		param.put("apkId", Md5Utils.getMd5(apkId.toString()));
		return param.toJSONString();
	}

}
